package com.sy.textin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sy.ocr.domain.BankCard;
import com.sy.ocr.domain.BusinessLicense;
import com.sy.ocr.domain.IdCard;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class TextInAnalysisUtil {

    public static void main(String[] args) {


        // idCard(null);
        // businessLicense(null);
        bankCard(null);
    }

    public static IdCard idCard(JSONObject jsonObject) {


        JSONObject object = JSONUtil.parseObj(FileUtil.readUtf8String("E:\\test\\img\\idcard.json"));

        IdCard idCard = new IdCard();


        JSONArray itemList = getItemList(object);


        if (Objects.isNull(itemList)) {
            return idCard;
        }
        Map<String, String> dataMap = new HashMap<>();
        for (Object o : itemList) {
            JSONObject data = (JSONObject) o;
            String key = data.getStr("key");
            String value = data.getStr("value");
            dataMap.put(StrUtil.toCamelCase(key), StrUtil.nullToEmpty(value));
        }
        BeanUtil.copyProperties(dataMap, idCard);
        return idCard;
    }


    public static JSONArray getItemList(JSONObject jsonObject) {
        JSONObject result = jsonObject.getJSONObject("result");
        if (Objects.isNull(result)) {
            return null;
        }
        return result.getJSONArray("item_list");
    }


    public static BusinessLicense businessLicense(JSONObject jsonObject) {
        JSONObject object = JSONUtil.parseObj(FileUtil.readUtf8String("E:\\test\\img\\businessLicense.json"));

        BusinessLicense businessLicense = new BusinessLicense();


        JSONArray itemList = getItemList(object);

        if (Objects.isNull(itemList)) {
            return businessLicense;
        }

        Map<String, String> dataMap = new HashMap<>();
        for (Object o : itemList) {
            JSONObject data = (JSONObject) o;
            String key = data.getStr("key");
            String value = data.getStr("value");
            String subAfter = StrUtil.subAfter(key, "BizLicense", false);
            String str = StrUtil.lowerFirst(subAfter);
            dataMap.put(str, StrUtil.nullToEmpty(value));
        }
        BeanUtil.copyProperties(dataMap, businessLicense);
        return businessLicense;
    }

    public static BankCard bankCard(JSONObject jsonObject) {
        JSONObject object = JSONUtil.parseObj(FileUtil.readUtf8String("E:\\test\\img\\bankCard.json"));

        BankCard bankCard = new BankCard();


        JSONArray itemList = getItemList(object);
        if (Objects.isNull(itemList)) {
            return bankCard;
        }

        Map<String, String> dataMap = new HashMap<>();
        for (Object o : itemList) {
            JSONObject data = (JSONObject) o;
            String key = data.getStr("key");
            String value = data.getStr("value");
            dataMap.put(StrUtil.toCamelCase(key), StrUtil.nullToEmpty(value));
        }
        BeanUtil.copyProperties(dataMap, bankCard);
        return bankCard;
    }
}
